package jvm;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author sjw
 * @className Test
 * @date 2021/10/13 9:53
 * @description: TODO
 */
public class Test {
    public static InheritableThreadLocal<Integer> threadLocal = new InheritableThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {
        Test.threadLocal.set(1);
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("sub");
                System.out.println(Test.threadLocal.get());
            }
        }).start();

        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 10; i++) {
            final int index = i;
            newFixedThreadPool.execute(() -> {

                System.out.println(Thread.currentThread().getId() + "\t" +index+"\t"+ Test.threadLocal.get());

            });
        }

        System.out.println(Test.threadLocal.get());
        Thread.sleep(1000);
        Test.threadLocal.set(11);
        for (int i = 0; i < 10; i++) {
            final int index = i;
            newFixedThreadPool.execute(() -> {
                Test.threadLocal.set(11);
                System.out.println(Thread.currentThread().getId() + "\t" +index+"\t"+ Test.threadLocal.get());
                Test.threadLocal.remove();
            });
        }
    }

}
